<template>
    <div :class="[$style.container]" v-loading="renderedLoading">
        <div :class="[$style.header]">
            <div>
                <el-button type="text" @click="onForward(-1)" :disabled="idx <= 0">上一个</el-button>
            </div>
            <div>
                <el-button type="text" class="mr-6" @click="expand = !expand">
                    <span v-if="expand">收起列表</span>
                    <span v-else>展开列表</span>
                </el-button>
                <el-dropdown>
                    <el-button type="text">
                        <span>{{ type.label }}</span>
                        <span class="ml-1">{{ idx + 1 }} / {{ len > 30 ? 30 : len }}</span>
                        <span class="ml-1"><i class="el-icon-arrow-down"></i></span>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item :class="[typeSelectedCls(row)]" v-for="(row, i) in types" :key="i" @click.native="onSelectType(row)">
                            <span class="mx-3">
                                <span class="ml-2">{{ row.label }}</span>
                            </span>
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <el-button type="text" class="ml-6" @click="onForward(1)" :disabled="idx >= len - 1">下一个</el-button>
            </div>
        </div>
        <div :class="[$style.body]">
            <ul :class="[$style.list]" v-show="expand" v-if="renderedRows.length > 0">
                <li :class="[$style.item, rowSelectedCls(row)]" v-for="(row, i) in renderedRows" :key="i" @click="onSelect(row, i)">
                    <div :class="[$style.icon]"><doc-icon :data="row"></doc-icon></div>
                    <div :class="[$style.text]" :title="row.name">{{ row.name }}</div>
                </li>
            </ul>
            <empty-data class="my-3" v-if="expand && renderedRows.length < 1">暂无文档</empty-data>
        </div>
    </div>
</template>

<script>
import EmptyData from '../../empty-data';
// import { getDocById, getUserDocs } from '../../api/doc';
import { Vue, Component, Prop, Emit } from 'vue-property-decorator';
import { getSuffixesByType, getType } from '../../config/content-type';
import { PageData } from '../../config/page';
import DocIcon from '../doc-icon';

const types = [
    { name: '', label: '全部' },
    { name: 'office', label: 'office文档' },
    { name: 'pdf', label: 'pdf' },
    { name: 'image', label: '图片' },
    { name: 'video', label: '视频' },
    { name: 'audio', label: '音频' },
    { name: 'text', label: '文本' }
];

// const eq = (o1, o2) => o1 === o2 || o1?.docId === o2?.docId;
const eq = (o1, o2) => o1 === o2 || o1?.annexId === o2?.annexId;

@Component({
    components: {
        EmptyData,
        DocIcon
    }
})
export default class FileGallery extends Vue {
    @Prop() parentId;

    @Prop() id;

    @Prop() versionId;

    @Prop() linkId;

    @Prop() accessCode;

    @Prop() current;
    // eslint-disable-next-line
    @Emit('update:current') updateCurrent(current) {}

    rows = [];

    loading = false;

    pageData = null;

    type = types[0];

    types = types;

    expand = true;

    doc = null;

    get renderedRows() {
        // 更新数据
        this.rows = this.$attrs['preview-rows'];
        const rows = this.pageData ? this.pageData.rows : this.rows;
        console.log('rows', rows);
        return rows.filter((v) => {
            if (!this.type?.name) return true;
            const type = getType(v.name);
            return type === this.type?.name;
        });
    }

    get len() {
        return this.renderedRows.length;
    }

    get idx() {
        return this.renderedRows.findIndex((v) => eq(this.current, v));
    }

    get renderedLoading() {
        return this.pageData ? this.pageData.loading : this.loading;
    }

    typeSelectedCls(row) {
        return row === this.type ? 'selected' : '';
    }

    rowSelectedCls(row) {
        return eq(row, this.current) ? this.$style.selected : '';
    }

    onSelectType(row) {
        this.type = row;
        // this.remoteLoadData();
        this.filterLoadData();
    }

    onSelect(row, i) {
        // if (this.pageData && i >= this.renderedRows.length - 1) {
        //     this.pageData.loadNext();
        // }
        // this.$emit('update-current', row);
        this.$emit('update-preview', row);
    }

    onForward(offset) {
        const n = Math.max(0, Math.min(this.idx + offset, this.len - 1));
        const item = this.renderedRows[n];
        if (item) this.onSelect(item, n);
    }

    loadData() {
        this.loading = true;
        if (this.versionId) {
            getHistoryDoc(this.versionId)
                .then((data) => {
                    this.rows = (data && [data]) || [];
                    this.updateCurrent(data || null);
                })
                .finally(() => {
                    this.loading = false;
                });
        } else if (this.parentId) {
            // 预览的时候,调用接口查询预览页面的数据
            // this.remoteLoadData();
        } else {
            //直接把文档列表传参过来
            this.loading = false;
            /* const request = this.linkId ? getDocByLink(Number(this.linkId), this.id) : getDocById(this.id);
            request
                .then((data) => {
                    this.rows = (data && [data]) || [];
                    this.updateCurrent(data || null);
                })
                .finally(() => {
                    this.loading = false;
                }); */
        }
    }

    /* 过滤列表数据 */
    filterLoadData() {
        this.pageData ?? (this.pageData = { rows: [] });
        this.pageData.rows = this.rows.filter((val) => {
            if (!this.type?.name) return true;
            const type = getType(val.name);
            return type === this.type?.name;
        });
        const bool = this.pageData.rows.some((val) => val === this.current || val?.annexId === this.current?.annexId);
        if (!bool && this.pageData.rows.length) this.onSelect(this.pageData.rows[0]);
    }

    loadDoc() {
        // if (this.id) {
        //     return getDocById(this.id).then((data) => {
        //         this.doc = data || null;
        //         return this.doc;
        //     });
        // }
        return Promise.resolve(null);
    }

    created() {
        this.loadData();
    }

    // $style!;
}
</script>

<style lang="scss" module>
@import '../../vars.scss';

$header-height: 32px;

.header {
    height: $header-height;
    padding: 0 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-top: 1px solid $border-split-color;
    box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.08);
}

.body {
    box-sizing: border-box;
    width: 100%;
}

.list {
    list-style: none;
    margin: 0;
    padding-top: 12px;
    box-sizing: border-box;
    overflow: auto;
    display: flex;
    width: 0;
    min-width: 100%;
}

.item {
    flex: 0 0 auto;
    height: 112px;
    width: 100px;
    margin-right: 8px;
    box-sizing: border-box;
    border: 1px solid #ecedf2;
    border-radius: 2px;

    display: flex;
    flex-direction: column;
    justify-content: space-between;

    &:hover {
        cursor: pointer;
        border-color: $text-placeholder-color;
    }

    &.selected {
        border-color: $color-primary;
    }
}

.icon {
    font-size: 48px;
    text-align: center;
}

.text {
    margin: 8px 0;
    font-size: 12px;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    line-height: 15px;
}
</style>
<style lang="scss" scoped>
.mr-6 {
    margin-right: 24px;
}
.ml-6 {
    margin-left: 24px;
}
.mx-3 {
    margin-right: 12px;
}
.ml-1 {
    margin-left: 4px;
}
.ml-2 {
    margin-left: 8px;
}
.my-3 {
    margin-bottom: 12px;
}
.el-dropdown-menu__item.selected {
    color: #1678ff;
}
::v-deep .el-button--text {
    border-color: transparent !important;
    color: #1678ff !important;
    font-size: 14px;
    .el-icon-arrow-down {
        color: #1678ff !important;
        font-size: 14px;
        margin-left: 2px;
    }
    &.is-disabled {
        border-color: transparent !important;
        color: #1678ff !important;
    }
    &:hover {
        background: transparent !important;
    }
}
</style>
